package netty.test15websocket;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
import netty.test14Hreats.MyServerIdleHandler;

import java.util.concurrent.TimeUnit;


public class MyServer {
    public static void main(String[] args) throws InterruptedException {
        //创建两个线程组
        EventLoopGroup bossGroup=new NioEventLoopGroup(1);
        EventLoopGroup workerGroup=new NioEventLoopGroup();//默认8 个
        try{
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG,128)
                    .childOption(ChannelOption.SO_KEEPALIVE,true)
                    .handler(new LoggingHandler(LogLevel.INFO))//日志处理器
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            //因为websocket 基于http的所以要使用http的编码解码器
                            pipeline.addLast(new HttpServerCodec());
                            //是以块方式写的，所以要添加ChunkedWriter处理器
                            pipeline.addLast(new ChunkedWriteHandler());
                            //http数据传输过程中是分段的，所以 HttpObjectAggregator 这个就是可以将多个
                            //段聚合起来。（当浏览器在发送大量数据时候会出现多次http请求）
                            pipeline.addLast(new HttpObjectAggregator(8192));
                            //对于websocket的数据是以Frame的形式传递的，所以要有一个处理器去处理。可以看到
                            //WebsockeFrame下面有六个子类，浏览器请求时，websocket的形式是  ws://localhost:7000/xxx(请求的url)
                            //所以要用 WebSocketServerProtocolHandler 这个来映射请求路径 比如上面的xxx是 hello的话，下面这个也要是hello
                            //WebSocketServerProtocolHandler 的核心功能是将http协议升级为ws协议，即websocket协议，保持长链接。

                            pipeline.addLast(new WebSocketServerProtocolHandler("/hello"));

                            //自定义的处理业务的handler.
                            pipeline.addLast(new MyServerTextWebSocketHandler());

                        }
                    });
            ChannelFuture sync = bootstrap.bind(7000).sync();
            sync.channel().closeFuture().sync();
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }
}
